
import  time
from selenium import  webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pyautogui




def nextBtn():
    try:
        # 查找要操作的元素
        WebDriverWait(driver, 30).until(
            EC.presence_of_element_located((By.XPATH, "//button"))
        )

        # 下一步按钮
        tbns = driver.find_elements(By.TAG_NAME, "button")

        for e in tbns:
            print(e.text)
            if e.text == '下一步' or e.text == '连接' or e.text == '批准' or e.text == '切换网络' or e.text == '明白了' or e.text == '签名' :
                print("点击了： ")
                print(e.text)
                e.click()
                time.sleep(2)
            else:
                print("没有找到匹配的点击按钮 ")
    except:
        print("没有下一步按钮，跳过。。。")



def passwordLogin():
    try:
        WebDriverWait(driver, 50).until(
            EC.presence_of_element_located((By.ID, "password"))
        )

        pwdElement = driver.find_element(By.ID, "password")

        #账号的密码
        pwdElement.send_keys("------------账号密码----------------")
        # 查找要登录的钱包点击
        driver.find_element(By.CSS_SELECTOR, "button[data-testid='unlock-submit']").click()

        nextBtn()
        nextBtn()
        nextBtn()

        time.sleep(2)
        if endCheck() :
            nextBtn()
            nextBtn()
            nextBtn()

        time.sleep(2)
        if endCheck() :
            nextBtn()
            nextBtn()
            nextBtn()
        return False
    except Exception as err:
        print(f"Unexpected {err=}, {type(err)=}")
        print("没有密码输入框，不用密码登录")
        return True


def endCheck():
    try:
        # 等待新窗口出现
        WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) > 1)

        # 切换窗口
        for window_handle in driver.window_handles:
            if window_handle != original_window:
                driver.switch_to.window(window_handle)
                break
        print("------有新窗口----，切换到新窗口-----")
        return True
    except:
        print("没有新窗口----")
        return False

#开始入口：  创建driver对象， 带插件的浏览器同一时间只能打开一个
option = webdriver.ChromeOptions()
option.add_argument("--user-data-dir="+r"C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/")

driver=webdriver.Chrome(options=option)
driver.maximize_window()  #窗口最大化
driver.get('https://protocol.carv.io/airdrop')  #打开网站


WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.XPATH,"//button[text()='Login']"))
    )

#登录按钮
driver.find_element(By.XPATH,"//button[text()='Login']").click()

time.sleep(1)

original_window = driver.current_window_handle
#查找要登录的钱包点击
driver.find_element(By.CSS_SELECTOR,"button[data-testid='rk-wallet-option-metaMask']").click()
#等待新窗口出现
WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) > 1)

#切换窗口
for window_handle in driver.window_handles:
    if window_handle != original_window:
        driver.switch_to.window(window_handle)
        break


# 等待出现可以选择钱包
try:
    WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.CLASS_NAME, "_wallet_qdhwz_1"))
    )
    elements = driver.find_elements(By.CLASS_NAME, "_wallet_qdhwz_1")

    #选择哪个钱包登录
    elements[1].click()

    time.sleep(5)

    #等待新窗口出现
    WebDriverWait(driver, 20).until(lambda d: len(d.window_handles) > 1)

    #切换窗口
    for window_handle in driver.window_handles:
        if window_handle != original_window:
            driver.switch_to.window(window_handle)
            break
except:
    print("没有钱包选择，直接登录")



print("---等待20秒，或者直到有密码输入框--")
#等待20秒，或者直到有密码输入框
if passwordLogin():
    time.sleep(1)
    # 切换窗口
    for window_handle in driver.window_handles:
        if window_handle != original_window:
            driver.switch_to.window(window_handle)
            break
    passwordLogin()


time.sleep(150)
